using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace Decav.Gatsb.Data
{
    /// <summary>
    /// Assists in throwing a detailed exception that is produced by the SQL server.
    /// </summary>
    public static class SqlThrowHelper
    {
        public static void ThrowException(SqlException ex)
        {
            if (ex == null)
                throw new ArgumentNullException("ex");

            switch ((SqlErrorCode)ex.Number)
            {
                case SqlErrorCode.NewAccountEmailExists:
                    throw new UserExistsException(ex.Message, ex);

                case SqlErrorCode.NewAccountMobileExists:
                    throw new UserExistsException(ex.Message, ex);

                case SqlErrorCode.UserDoesNotExist:
                    throw new UserDoesNotExistException(ex.Message, ex);

                case SqlErrorCode.LocationDoesNotExist:
                    throw new LocationDoesNotExistException(ex.Message, ex);

                case SqlErrorCode.DataAccessDenied:
                    throw new DataAccessDeniedException(ex.Message, ex);
            }

            if (ex.Number > 50000)
                throw new GatsbDataException(ex.Message, ex);
        }

        /// <summary>
        /// Enumerates the custom error codes used in the SQL database.
        /// </summary>
        public enum SqlErrorCode
        {
            NewAccountEmailExists = 50101,
            NewAccountMobileExists = 50102,
            UserDoesNotExist = 50111,
            EmailPasswordDoesNotExist = 50112,
            LocationDoesNotExist = 50121,
            DataAccessDenied = 50500
        }
    }
}
